package com.enfi.api.modules.db.service.impl;

import com.enfi.api.modules.db.base.BaseQuery;
import com.enfi.api.modules.db.base.BaseServiceImpl;
import com.enfi.api.modules.db.dao.WorkorderRepository;
import com.enfi.api.modules.db.dao.WorkorderUserRepository;
import com.enfi.api.modules.db.entity.Warning;
import com.enfi.api.modules.db.entity.Workorder;
import com.enfi.api.modules.db.entity.WorkorderUser;
import com.enfi.api.modules.db.service.WorkorderService;
import com.enfi.api.modules.db.service.WorkorderUserService;
import com.enfi.api.util.SecUser;
import com.enfi.api.util.SpeUtils;
import com.enfi.api.util.UserUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 待办列表 包含财务审批和工作联络单(Workorder)表服务实现类
 *
 * @author wind
 * @since 2020-04-10 12:01:45
 */
@Service("workorderService")
@NoRepositoryBean
public class WorkorderServiceImpl extends BaseServiceImpl<WorkorderRepository,Workorder,Integer> implements WorkorderService {
    @Autowired
    private WorkorderUserService workorderUserService;
    @Override
    protected Object query(BaseQuery baseQuery, Workorder workorder, Pageable pageable) {
        Assert.isTrue(baseQuery.getQuery()!=null&&baseQuery.getQuery().getQueryType()!=null,"查询方式不为空");
        switch (baseQuery.getQuery().getQueryType()){
            case "":
                SecUser secUser= UserUtils.getSecUser();
                Page<Workorder> page= this.baseRepository.findAll((root,query,builder)->{
                    List<Predicate> predicates= SpeUtils.getInstance().toSpec(workorder,baseQuery,root,builder);
                    if(secUser!=null){
                        Join join = root.join("workorderUsers", JoinType.LEFT);
                        predicates.add(builder.and(builder.equal(join.get("uuid"), root.get("uuid"))));
                        predicates.add(builder.and(builder.equal(join.get("userId"), secUser.getId())));
                    }
                    query.where(predicates.stream().toArray(Predicate[]::new));
                    return null;
                },pageable);
                List<Integer> workorderIdList=page.getContent().stream().map(v->v.getId()).collect(Collectors.toList());
                Map<Integer,WorkorderUser> map=this.workorderUserService.findAll((root, query, builder)->{
                    List<Predicate> predicates= Lists.newArrayList();
                    predicates.add(root.get("workorderId").in(workorderIdList));
                    predicates.add(builder.equal(root.get("userId"),secUser.getId()));
                    query.where(predicates.stream().toArray(Predicate[]::new));
                    return  null;
                }).stream().collect(Collectors.toMap(WorkorderUser::getWorkorderId, Function.identity(), (key1, key2) -> key2));
                page.getContent().forEach(v->{
                    if(map.containsKey(v.getId())){
                        v.setWorkorderUser(map.get(v.getId()));
                    }
                });
                return page;
            default:
                return super.query(baseQuery,workorder,pageable);
        }
    }
}